#
# This file is a part of Pcompress, a chunked parallel multi-
# algorithm lossless compression and decompression program.
#
# Copyright (C) 2012-2013 Moinak Ghosh. All rights reserved.
# Use is subject to license terms.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
# moinakg@belenix.org, http://moinakg.wordpress.com/
#

PROG= pcompress
SO_SUFFIX=@SO_SUFFIX@
LIB=libpcompress.$(SO_SUFFIX)
LINKLIB=pcompress
GCC=@GCC@
GPP=@GPP@
LIBVER=1
MAINSRCS = utils/utils.c allocator.c lzma_compress.c ppmd_compress.c \
	adaptive_compress.c lzfx_compress.c lz4_compress.c none_compress.c \
	utils/xxhash_base.c utils/heap.c utils/cpuid.c pcompress.c
MAINHDRS = allocator.h  pcompress.h  utils/utils.h utils/xxhash.h utils/heap.h \
	utils/cpuid.h utils/xxhash.h archive/pc_archive.h filters/dispack/dis.hpp
MAINOBJS = $(MAINSRCS:.c=.o)

PROGSRCS = main.c
PROGHDRS = pcompress.h  utils/utils.h
PROGOBJS = $(PROGSRCS:.c=.o)

XSALSA20_STREAM_C = crypto/xsalsa20/stream.c
XSALSA20_STREAM_ASM = crypto/xsalsa20/stream.s
XSALSA20_DEBUG = -DSALSA20_DEBUG

CRYPTO_SRCS = crypto/aes/crypto_aes.c crypto/scrypt/crypto_scrypt-nosse.c \
	crypto/scrypt/sha256.c crypto/scrypt/crypto_aesctr.c crypto/crypto_utils.c \
	crypto/sha2_utils.c crypto/sha3_utils.c crypto/xsalsa20/xsalsa20_xor.c \
	crypto/xsalsa20/hsalsa_core.c @XSALSA20_STREAM_C@
CRYPTO_HDRS = crypto/crypto_utils.h crypto/scrypt/crypto_scrypt.h \
	crypto/scrypt/sha256.h crypto/scrypt/crypto_aesctr.h crypto/aes/crypto_aes.h \
	crypto/sha2_utils.h crypto/sha3_utils.h crypto/xsalsa20/crypto_core_hsalsa20.h \
	crypto/xsalsa20/crypto_stream_salsa20.h crypto/xsalsa20/crypto_xsalsa20.h \
	$(MAINHDRS)
CRYPTO_ASM_SRCS = crypto/aes/vpaes-x86_64.s crypto/aes/aesni-x86_64.s @XSALSA20_STREAM_ASM@
CRYPTO_ASM_OBJS = $(CRYPTO_ASM_SRCS:.s=.o)
CRYPTO_ASM_HDRS = crypto/aes/crypto_aes.h crypto/xsalsa20/crypto_stream_salsa20.h
CRYPTO_COMPAT_SRCS = crypto/old/sha2_utils_old.c crypto/old/sha3_utils_old.c
CRYPTO_COMPAT_HDRS = crypto/old/sha2_utils_old.h crypto/old/sha3_utils_old.h
CRYPTO_OBJS = $(CRYPTO_SRCS:.c=.o)
CRYPTO_COMPAT_OBJS = $(CRYPTO_COMPAT_SRCS:.c=.o)
CRYPTO_CPPFLAGS=-I@OPENSSL_INCDIR@

XXHASH_SRCS = utils/xxhash.c
XXHASH_SSE4_SRCS = utils/xxhash_sse4.c
XXHASH_SSE2_SRCS = utils/xxhash_sse2.c
XXHASH_OBJS = utils/xxhash_sse4.o utils/xxhash_sse2.o
XXHASH_HDRS = utils/xxhash.h

BLAKE2b_SSE2 = crypto/blake2/blake2b_sse2.c
BLAKE2b_SSE3 = crypto/blake2/blake2b_ssse3.c
BLAKE2b_SSE4 = crypto/blake2/blake2b_sse41.c
BLAKE2b_AVX = crypto/blake2/blake2b_avx.c
BLAKE2bp_SSE2 = crypto/blake2/blake2bp_sse2.c
BLAKE2bp_SSE3 = crypto/blake2/blake2bp_ssse3.c
BLAKE2bp_SSE4 = crypto/blake2/blake2bp_sse41.c
BLAKE2bp_AVX = crypto/blake2/blake2bp_avx.c
BLAKE2_BASE_SRCS = crypto/blake2/blake2b.c crypto/blake2/blake2bp.c
BLAKE2_HDRS = crypto/blake2/blake2.h crypto/blake2/blake2-impl.h crypto/blake2/blake2-config.h \
	crypto/blake2/blake2-kat.h crypto/blake2/blake2b-round.h crypto/blake2/blake2b-load-sse2.h \
	crypto/blake2/blake2b-load-sse41.h
BLAKE2_SRCS = $(BLAKE2b_SSE2) $(BLAKE2b_SSE3) $(BLAKE2b_SSE4) $(BLAKE2b_AVX) \
	$(BLAKE2bp_SSE2) $(BLAKE2bp_SSE3) $(BLAKE2bp_SSE4) $(BLAKE2bp_AVX)
BLAKE2_OBJS = $(BLAKE2_SRCS:.c=.o)

ZLIB_SRCS = zlib_compress.c
ZLIB_HDRS = $(MAINHDRS)
ZLIB_OBJS = $(ZLIB_SRCS:.c=.o)
ZLIB_CPPFLAGS = @LIBZ_INC@

BZLIB_SRCS = bzip2_compress.c
BZLIB_HDRS = $(MAINHDRS)
BZLIB_OBJS = $(BZLIB_SRCS:.c=.o)
BZLIB_CPPFLAGS = @LIBBZ2_INC@

RABINSRCS = rabin/rabin_dedup.c rabin/global/index.c rabin/global/dedupe_config.c
RABINHDRS = rabin/rabin_dedup.h utils/utils.h rabin/global/index.h rabin/global/dedupe_config.h lzma/lzma_crc.h utils/qsort.h
RABINOBJS = $(RABINSRCS:.c=.o)

BSDIFFSRCS = bsdiff/bsdiff.c bsdiff/bspatch.c bsdiff/rle_encoder.c
BSDIFFHDRS = bsdiff/bscommon.h utils/utils.h allocator.h
BSDIFFOBJS = $(BSDIFFSRCS:.c=.o)

LZMASRCS = lzma/LzmaEnc.c lzma/LzFind.c lzma/LzmaDec.c lzma/Threads.c lzma/LzFindMt.c
LZMAHDRS = lzma/CpuArch.h lzma/LzFind.h lzma/LzmaEnc.h lzma/Types.h \
	lzma/LzHash.h lzma/LzmaDec.h utils/utils.h lzma/LzFindMt.h lzma/Threads.h lzma/windows.h \
	lzma/Common/MyWindows.h lzma/Common/MyGuidDef.h lzma/basetyps.h
LZMAOBJS = $(LZMASRCS:.c=.o)

LZFXSRCS = lzfx/lzfx.c
LZFXHDRS = lzfx/lzfx.h
LZFXOBJS = $(LZFXSRCS:.c=.o)

LZ4SRCS = lz4/lz4.c lz4/lz4hc.c
LZ4HDRS = lz4/lz4.h lz4/lz4hc.h
LZ4OBJS = $(LZ4SRCS:.c=.o)

PPMDSRCS = lzma/Ppmd8.c lzma/Ppmd8Enc.c lzma/Ppmd8Dec.c
PPMDHDRS = lzma/Ppmd.h lzma/Ppmd8.h
PPMDOBJS = $(PPMDSRCS:.c=.o)

CRCSRCS = lzma/crc64_fast.c lzma/crc64_table.c lzma/crc32_fast.c lzma/crc32_table.c
CRCHDRS = lzma/crc64_table_le.h lzma/crc64_table_be.h lzma/crc_macros.h \
	lzma/crc32_table_le.h lzma/crc32_table_be.h lzma/lzma_crc.h
CRCOBJS = $(CRCSRCS:.c=.o)

LZPSRCS = filters/lzp/lzp.c
LZPHDRS = filters/lzp/lzp.h
LZPOBJS = $(LZPSRCS:.c=.o)

DELTA2SRCS = filters/delta2/delta2.c
DELTA2HDRS = filters/delta2/delta2.h
DELTA2OBJS = $(DELTA2SRCS:.c=.o)

ARCHIVESRCS = archive/pc_archive.c archive/pc_arc_filter.c utils/phash/phash.c \
	utils/phash/lookupa.c utils/phash/recycle.c
ARCHIVEHDRS = pcompress.h  utils/utils.h archive/pc_archive.h utils/phash/standard.h \
	utils/phash/lookupa.h utils/phash/recycle.h utils/phash/phash.h archive/pc_arc_filter.h \
	utils/phash/extensions.h
ARCHIVEOBJS = $(ARCHIVESRCS:.c=.o)

PJPGSRCS = filters/packjpg/aricoder.cpp filters/packjpg/bitops.cpp filters/packjpg/packjpg.cpp \
	 archive/pjpg_helper.cpp
PJPGHDRS = filters/packjpg/aricoder.h filters/packjpg/bitops.h filters/packjpg/dct8x8.h \
	filters/packjpg/packjpglib.h filters/packjpg/pjpgtbl.h
PJPGOBJS = $(PJPGSRCS:.cpp=.o)

DISPACKSRCS = filters/dispack/dis.cpp
DISPACKHDRS = filters/dispack/dis.hpp filters/dispack/types.hpp
DISPACKOBJS = $(DISPACKSRCS:.cpp=.o)

SKEIN_BLOCK_C = crypto/skein/skein_block.c
SKEIN_BLOCK_ASM = crypto/skein/skein_block_x64.s
SKEIN_BLOCK_SRC = @SKEIN_BLOCK@
SKEIN_BLOCK_OBJ = crypto/skein/skein_block.o
SKEINSRCS = crypto/skein/SHA3api_ref.c crypto/skein/skein.c crypto/skein/skein_debug.c
SKEINHDRS = crypto/skein/brg_endian.h crypto/skein/SHA3api_ref.h \
	crypto/skein/skein.h crypto/skein/skein_port.h crypto/skein/brg_types.h \
	crypto/skein/skein_debug.h crypto/skein/skein_iv.h
SKEINOBJS = $(SKEINSRCS:.c=.o)

SHA2_SRCS = crypto/sha2/sha512.c
SHA2_HDRS = crypto/sha2/sha512.h
SHA2ASM_SRCS = crypto/sha2/intel/sha512_avx.asm crypto/sha2/intel/sha512_sse4.asm
SHA2ASM_OBJS = $(SHA2ASM_SRCS:.asm=.o)
SHA2_OBJS = $(SHA2_SRCS:.c=.o)

YASM = @YASM@
YASM_GAS = @YASM_GAS@
LIBBSCWRAP = libbsc_compress.c
LIBBSCWRAPOBJ = libbsc_compress.o
LIBBSCDIR = @LIBBSCDIR@
LIBBSCLFLAGS = -L$(LIBBSCDIR) -lbsc
LIBBSCLIB = @LIBBSCLIB@
LIBBSCGEN_OPT = -fopenmp
LIBBSCCPPFLAGS = -I$(LIBBSCDIR)/libbsc -DENABLE_PC_LIBBSC

TRANSP_SRCS = filters/transpose/transpose.c
TRANSP_HDRS = filters/transpose/transpose.h
TRANSP_OBJS = $(TRANSP_SRCS:.c=.o)

KECCAK_SRC_COMMON = crypto/keccak/genKAT.c crypto/keccak/KeccakDuplex.c \
	crypto/keccak/KeccakNISTInterface.c crypto/keccak/KeccakSponge.c
KECCAK_SRC_OPT64 = $(KECCAK_SRC_COMMON) crypto/keccak/KeccakF-1600-opt64.c
KECCAK_SRC_OPT64_ASM1 = $(KECCAK_SRC_COMMON) crypto/keccak/KeccakF-1600-x86-64-asm.c
KECCAK_SRC_OPT64_ASM2 = crypto/keccak/KeccakF-1600-x86-64-gas.s
KECCAK_SRC_OPT64_ASM3 = crypto/keccak/KeccakF-1600-x86-64-gas_yasm.s
KECCAK_OBJS_OPT64_ASM2 = $(KECCAK_SRC_OPT64_ASM2:.s=.o)
KECCAK_OBJS_OPT64_ASM3 = $(KECCAK_SRC_OPT64_ASM3:.s=.o)

KECCAK_HDRS_COMMON = crypto/keccak/KeccakDuplex.h crypto/keccak/KeccakNISTInterface.h \
	crypto/keccak/KeccakSponge.h crypto/keccak/KeccakF-1600-interface.h
KECCAK_HDRS_OPT = $(KECCAK_HDRS_COMMON) \
	crypto/keccak/brg_endian.h crypto/keccak/KeccakF-1600-unrolling.macros
KECCAK_HDRS_OPT64 = $(KECCAK_HDRS_OPT)  crypto/keccak/KeccakF-1600-opt64-settings.h \
	crypto/keccak/KeccakF-1600-64.macros  crypto/keccak/KeccakF-1600-simd64.macros \
	crypto/keccak/KeccakF-1600-simd128.macros
KECCAK_HDRS_OPT64_ASM = $(KECCAK_HDRS_OPT64)

KECCAK_SRCS = @KECCAK_SRCS@
KECCAK_SRCS_ASM = @KECCAK_SRCS_ASM@
KECCAK_HDRS = @KECCAK_HDRS@
KECCAK_OBJS = $(KECCAK_SRCS:.c=.o)
KECCAK_OBJS_ASM = $(KECCAK_SRCS_ASM:.s=.o)

CRYPTO_ASM_COMPILE1 = $(COMPILE) $(GEN_OPT) $(CRYPTO_CPPFLAGS) $(CPPFLAGS)
CRYPTO_ASM_COMPILE2 = $(YASM_GAS)
CRYPTO_ASM_COMPILE = @CRYPTO_ASM_COMPILE@

BAKFILES = *~ lzma/*~ lzfx/*~ lz4/*~ rabin/*~ bsdiff/*~ filters/lzp/*~ utils/*~ crypto/sha2/*~ \
	crypto/sha2/intel/*~ crypto/aes/*~ crypto/scrypt/*~ crypto/*~ rabin/global/*~ \
	delta2/*~ crypto/keccak/*~ transpose/*~ crypto/skein/*~ crypto/keccak/*.o \
	archive/*~ filters/delta2/*~ filters/packjpg/*~ filters/transpose/*~

RM = rm -f
RM_RF = rm -rf
BASE_CPPFLAGS = -I. -I./lzma -I./lzfx -I./lz4 -I./rabin -I./bsdiff -DNODEFAULT_PROPS \
	-DFILE_OFFSET_BITS=64 -D_REENTRANT -D__USE_SSE_INTRIN__ -D_LZMA_PROB32 \
	-I./filters/lzp @LIBBSCCPPFLAGS@ -I./crypto/skein -I./utils -I./crypto/sha2 \
	-I./crypto/scrypt -I./crypto/aes -I./crypto @KEYLEN@ -I./rabin/global \
	-I./crypto/keccak -I./filters/transpose -I./crypto/blake2 $(EXTRA_CPPFLAGS) \
	-I./crypto/xsalsa20 -I./archive -pedantic -Wall -I./filters -fno-strict-aliasing \
	-Wno-unused-but-set-variable -Wno-enum-compare \
	@COMPAT_CPPFLAGS@ @XSALSA20_DEBUG@ @LIBARCHIVE_INC@ -I./filters/packjpg
COMMON_CPPFLAGS = $(BASE_CPPFLAGS) -std=gnu99 
COMMON_CPPFLAGS_cpp = $(BASE_CPPFLAGS)
COMMON_VEC_FLAGS = -ftree-vectorize
COMMON_LOOP_OPTFLAGS = $(VEC_FLAGS) -floop-interchange -floop-block
RPATH=@RPATH@
DTAGS=@DTAGS@
LDLIBS = -ldl -L./buildtmp -Wl,$(RPATH)@LIBBZ2_DIR@ -lbz2 -L./buildtmp -Wl,$(RPATH)@LIBZ_DIR@ -lz -lm @LIBBSCLFLAGS@ \
	-L./buildtmp -Wl,$(RPATH)@OPENSSL_LIBDIR@ -lcrypto @LRT@ -Wl,$(RPATH)@LIBARCHIVE_DIR@ -larchive $(EXTRA_LDFLAGS) \
	-Wl,$(RPATH)/usr/lib$(DTAGS) -Wl,$(RPATH)/usr/lib64$(DTAGS)
OBJS = $(MAINOBJS) $(LZMAOBJS) $(PPMDOBJS) $(LZFXOBJS) $(LZ4OBJS) $(CRCOBJS) \
$(RABINOBJS) $(BSDIFFOBJS) $(LZPOBJS) $(DELTA2OBJS) @LIBBSCWRAPOBJ@ $(SKEINOBJS) \
$(SKEIN_BLOCK_OBJ) @SHA2ASM_OBJS@ @SHA2_OBJS@ $(KECCAK_OBJS) $(KECCAK_OBJS_ASM) \
$(TRANSP_OBJS) $(CRYPTO_OBJS) $(ZLIB_OBJS) $(BZLIB_OBJS) $(XXHASH_OBJS) $(BLAKE2_OBJS) \
@CRYPTO_COMPAT_OBJS@ $(CRYPTO_ASM_OBJS) $(ARCHIVEOBJS) $(PJPGOBJS) $(DISPACKOBJS)

DEBUG_LINK = $(GPP) -pthread @LIBBSCGEN_OPT@ @EXTRA_OPT_FLAGS@ -fopenmp -fPIC
DEBUG_COMPILE = $(GCC) -g -c @EXTRA_OPT_FLAGS@ -fPIC @USE_CLANG_AS@
DEBUG_COMPILE_cpp = $(GPP) -g -c @EXTRA_OPT_FLAGS@ -fPIC @USE_CLANG_AS@
DEBUG_VEC_FLAGS = 
DEBUG_LOOP_OPTFLAGS = 
DEBUG_GEN_OPT = -O -fno-omit-frame-pointer @LIBBSCGEN_OPT@ -fopenmp
DEBUG_CPPFLAGS = $(COMMON_CPPFLAGS)
DEBUG_FPTR_FLAG = 

RELEASE_LINK = $(GPP) -pthread @LIBBSCGEN_OPT@ @EXTRA_OPT_FLAGS@ -fopenmp -fPIC
RELEASE_COMPILE = $(GCC) -c @EXTRA_OPT_FLAGS@ -fPIC -fvisibility=hidden @USE_CLANG_AS@
RELEASE_COMPILE_cpp = $(GPP) -c @EXTRA_OPT_FLAGS@ -fPIC -fvisibility=hidden @USE_CLANG_AS@
RELEASE_VEC_FLAGS = $(COMMON_VEC_FLAGS)
RELEASE_LOOP_OPTFLAGS = $(COMMON_LOOP_OPTFLAGS)
RELEASE_CPPFLAGS = $(COMMON_CPPFLAGS) -DNDEBUG
RELEASE_GEN_OPT = -O3 @LIBBSCGEN_OPT@ -fopenmp
RELEASE_FPTR_FLAG = -fomit-frame-pointer

NO_SLAB_CPPFLAGS = -DDEBUG_NO_SLAB
DEBUG_STATS_CPPFLAGS = -DDEBUG_STATS

LINK.PROG = @LINK@
LINK.LIB = @LINK@ -shared
COMPILE = @COMPILE@
COMPILE_cpp = @COMPILE_cpp@
VEC_FLAGS = @VEC_FLAGS@
LOOP_OPTFLAGS = @LOOP_OPTFLAGS@
CPPFLAGS = @CPPFLAGS@ @NO_SLAB_CPPFLAGS@ @DEBUG_STATS_CPPFLAGS@
GEN_OPT = @GEN_OPT@ @SSE_OPT_FLAGS@
BASE_OPT = @GEN_OPT@
PREFIX=@PREFIX@
AVX_OPT_FLAG = -mavx @USE_CLANG_AS@
SSE4_OPT_FLAG = -msse4.2 @USE_CLANG_AS@
SSE3_OPT_FLAG = -mssse3 @USE_CLANG_AS@
SSE2_OPT_FLAG = -msse2 @USE_CLANG_AS@

SKEIN_FLAGS = $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) @FPTR_FLAG@
SHA2_FLAGS = $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) @FPTR_FLAG@
KECCAK_FLAGS = $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) @FPTR_FLAG@

all: $(PROG)

$(LZMAOBJS): $(LZMASRCS) $(LZMAHDRS)
	$(COMPILE) $(GEN_OPT) $(CPPFLAGS) $(@:.o=.c) -o $@

$(CRCOBJS): $(CRCSRCS) $(CRCHDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(PPMDOBJS): $(PPMDSRCS) $(PPMDHDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(RABINOBJS): $(RABINSRCS) $(RABINHDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(BSDIFFOBJS): $(BSDIFFSRCS) $(BSDIFFHDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(LZFXOBJS): $(LZFXSRCS) $(LZFXHDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(LZ4OBJS): $(LZ4SRCS) $(LZ4HDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(LZPOBJS): $(LZPSRCS) $(LZPHDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(DELTA2OBJS): $(DELTA2SRCS) $(DELTA2HDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(ARCHIVEOBJS): $(ARCHIVESRCS) $(ARCHIVEHDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(PJPGOBJS): $(PJPGSRCS) $(PJPGHDRS)
	$(COMPILE_cpp) $(COMMON_VEC_FLAGS) @SSE_OPT_FLAGS@ @USE_CLANG_AS@ -O2 -fsched-spec-load \
	$(VEC_FLAGS) -DBUILD_LIB $(COMMON_CPPFLAGS_cpp) $(@:.o=.cpp) -o $@

$(DISPACKOBJS): $(DISPACKSRCS) $(DISPACKHDRS)
	$(COMPILE_cpp) $(COMMON_VEC_FLAGS) @DEBUG_STATS_CPPFLAGS@ @SSE_OPT_FLAGS@ @USE_CLANG_AS@ -O2 -fsched-spec-load \
	-Wno-variadic-macros $(VEC_FLAGS) $(COMMON_CPPFLAGS_cpp) $(@:.o=.cpp) -o $@

$(SKEIN_BLOCK_OBJ): $(SKEIN_BLOCK_SRC)
	$(COMPILE) $(SKEIN_FLAGS) $(SKEIN_BLOCK_SRC) -o $@

$(SKEINOBJS): $(SKEINSRCS) $(SKEINHDRS)
	$(COMPILE) $(SKEIN_FLAGS) $(@:.o=.c) -o $@

$(SHA2_OBJS): $(SHA2_SRCS) $(SHA2_HDRS)
	$(COMPILE) $(SHA2_FLAGS) $(@:.o=.c) -o $@

$(SHA2ASM_OBJS): $(SHA2ASM_SRCS)
	$(YASM)	-o $@ $(@:.o=.asm)

$(KECCAK_OBJS): $(KECCAK_SRCS) $(KECCAK_HDRS)
	$(COMPILE) $(KECCAK_FLAGS) $(@:.o=.c) -o $@

$(KECCAK_OBJS_OPT64_ASM2): $(KECCAK_SRC_OPT64_ASM2)
	$(COMPILE) $(KECCAK_FLAGS) $(@:.o=.s) -o $@

$(KECCAK_OBJS_OPT64_ASM3): $(KECCAK_SRC_OPT64_ASM3)
	$(YASM_GAS) $(@:.o=.s) -o $@

$(KECCAK_OBJS_ASM): $(KECCAK_SRCS_ASM) $(KECCAK_HDRS)

$(LIBBSCWRAPOBJ): $(LIBBSCWRAP)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(TRANSP_OBJS): $(TRANSP_SRCS) $(TRANSP_HDRS)
	$(COMPILE) $(GEN_OPT) $(VEC_FLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(CRYPTO_OBJS): $(CRYPTO_SRCS) $(CRYPTO_HDRS) $(CRYPTO_ASM_OBJS)
	$(COMPILE) $(GEN_OPT) $(CRYPTO_CPPFLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(CRYPTO_ASM_OBJS): $(CRYPTO_ASM_SRCS) $(CRYPTO_ASM_HDRS)
	$(CRYPTO_ASM_COMPILE) -o $@ $(@:.o=.s)

$(CRYPTO_COMPAT_OBJS): $(CRYPTO_COMPAT_SRCS) $(CRYPTO_COMPAT_HDRS)
	$(COMPILE) $(GEN_OPT) $(CRYPTO_CPPFLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(ZLIB_OBJS): $(ZLIB_SRCS) $(ZLIB_HDRS)
	$(COMPILE) $(GEN_OPT) $(ZLIB_CPPFLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(BZLIB_OBJS): $(BZLIB_SRCS) $(BZLIB_HDRS)
	$(COMPILE) $(GEN_OPT) $(BZLIB_CPPFLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(XXHASH_OBJS): $(XXHASH_SSE4_SRCS) $(XXHASH_SSE2_SRCS) $(XXHASH_HDRS) $(XXHASH_SRCS)
	$(COMPILE) $(BASE_OPT) $(SSE4_OPT_FLAG) $(CPPFLAGS) $(XXHASH_SSE4_SRCS) -o $(XXHASH_SSE4_SRCS:.c=.o)
	$(COMPILE) $(BASE_OPT) $(SSE2_OPT_FLAG) $(CPPFLAGS) $(XXHASH_SSE2_SRCS) -o $(XXHASH_SSE2_SRCS:.c=.o)

$(BLAKE2_OBJS): $(BLAKE2_SRCS) $(BLAKE2_BASE_SRCS) $(BLAKE2_HDRS)
	$(COMPILE) $(BASE_OPT) $(SSE2_OPT_FLAG) $(CPPFLAGS) $(BLAKE2b_SSE2) -o $(BLAKE2b_SSE2:.c=.o)
	$(COMPILE) $(BASE_OPT) $(SSE3_OPT_FLAG) $(CPPFLAGS) $(BLAKE2b_SSE3) -o $(BLAKE2b_SSE3:.c=.o)
	$(COMPILE) $(BASE_OPT) $(SSE4_OPT_FLAG) $(CPPFLAGS) $(BLAKE2b_SSE4) -o $(BLAKE2b_SSE4:.c=.o)
	$(COMPILE) $(BASE_OPT) $(AVX_OPT_FLAG) $(CPPFLAGS) $(BLAKE2b_AVX) -o $(BLAKE2b_AVX:.c=.o)
	$(COMPILE) $(BASE_OPT) $(SSE2_OPT_FLAG) $(CPPFLAGS) $(BLAKE2bp_SSE2) -o $(BLAKE2bp_SSE2:.c=.o)
	$(COMPILE) $(BASE_OPT) $(SSE3_OPT_FLAG) $(CPPFLAGS) $(BLAKE2bp_SSE3) -o $(BLAKE2bp_SSE3:.c=.o)
	$(COMPILE) $(BASE_OPT) $(SSE4_OPT_FLAG) $(CPPFLAGS) $(BLAKE2bp_SSE4) -o $(BLAKE2bp_SSE4:.c=.o)
	$(COMPILE) $(BASE_OPT) $(AVX_OPT_FLAG) $(CPPFLAGS) $(BLAKE2bp_AVX) -o $(BLAKE2bp_AVX:.c=.o)

$(MAINOBJS): $(MAINSRCS) $(MAINHDRS)
	$(COMPILE) $(GEN_OPT) $(LOOP_OPTFLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(PROGOBJS): $(PROGSRCS) $(PROGHDRS)
	$(COMPILE) $(GEN_OPT) $(LOOP_OPTFLAGS) $(CPPFLAGS) $(@:.o=.c) -o $@

$(LIBBSCLIB):
	(cd $(LIBBSCDIR); make CC=$(GPP) OPTFLAGS="-O3 @SSE_OPT_FLAGS@ @USE_CLANG_AS@")

$(LIB): $(OBJS)  $(LIBBSCLIB)
	$(LINK.LIB) -Wl,-soname$(LIB).$(LIBVER) -o $(LIB).$(LIBVER) $(OBJS) $(LDLIBS)
	ln -sf $(LIB).$(LIBVER) $(LIB)

./buildtmp/$(PROG):
	mkdir -p buildtmp
	$(LINK.PROG) -o $@ $(PROGOBJS) $(LDLIBS) -L. -l$(LINKLIB)

$(PROG): $(LIB) $(PROGOBJS) ./buildtmp/$(PROG)
	cat utils/pcompress.sh | sed "s#<PC_PATH>#`pwd`#" > pcompress
	chmod +x pcompress

test: all
	(cd test; ulimit -c unlimited; sh ./run_test.sh $(TESTSUITE) ) 2>&1 | tee test.log

clean:
	$(RM) buildtmp/$(PROG) $(OBJS) $(PROGOBJS) $(BAKFILES) $(LIB) $(LIB).$(LIBVER)
	$(RM) test.log
	$(RM_RF) test/datafiles

distclean: clean
	$(RM) Makefile
	(cd $(LIBBSCDIR); make clean)

install: $(PROG)
	@mkdir -p $(DESTDIR)$(PREFIX)/bin
	@chmod 0755 $(DESTDIR)$(PREFIX)/bin
	@cp buildtmp/$(PROG) $(DESTDIR)$(PREFIX)/bin
	@chmod 0555 $(DESTDIR)$(PREFIX)/bin/$(PROG)

	@mkdir -p $(DESTDIR)$(PREFIX)/lib
	@chmod 0755 $(DESTDIR)$(PREFIX)/lib
	@cp $(LIB).$(LIBVER) $(DESTDIR)$(PREFIX)/lib
	@chmod 0555 $(DESTDIR)$(PREFIX)/lib/$(LIB).$(LIBVER)
	@ln -sf $(LIB).$(LIBVER) $(DESTDIR)$(PREFIX)/lib/$(LIB)

	@mkdir -p $(DESTDIR)$(PREFIX)/share/doc/$(PROG)
	@chmod 0755 $(DESTDIR)$(PREFIX)/share $(DESTDIR)$(PREFIX)/share/doc $(DESTDIR)$(PREFIX)/share/doc/$(PROG)
	@cp README.md $(DESTDIR)$(PREFIX)/share/doc/$(PROG)/README
	@chmod 0444 $(DESTDIR)$(PREFIX)/share/doc/$(PROG)/README


